package com.noble.meether.chat.ui.system.store;

import android.content.ContentValues;
import android.database.Cursor;
import android.text.TextUtils;

import com.noble.meether.chat.ui.system.bean.SysNotice;
import com.noble.meether.db.utils.CursorUtil;

import rx.functions.Func1;

/**
 * 公告信息
 */
public class NoticeTable {
    public static final String TABLE_NAME = "notice";   // 表名

    // 消息体字段
    public static final String COLUMN_ID = "Nid";        // id
    public static final String COLUMN_TITLE = "Title";
    public static final String COLUMN_IMAGE = "Image";
    public static final String COLUMN_TEXT = "Text";
    public static final String COLUMN_URL = "Url";
    public static final String COLUMN_TIME = "TimeCreate";

    // 自定义字段
    public static final String ID = "id";                         // 自增id

    // 建表语句
    public static String getCreateTable() {
        return "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + "(" +
                ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
                COLUMN_ID + " INT," +
                COLUMN_TITLE + " TEXT," +
                COLUMN_IMAGE + " TEXT," +
                COLUMN_TEXT + " TEXT," +
                COLUMN_URL + " TEXT," +
                COLUMN_TIME + " TEXT);";
    }

    // 根据表中的row生成一个对象
    public static Func1<Cursor, SysNotice> MAPPER = cursor -> {
        SysNotice data = new SysNotice();
        data.setId(CursorUtil.getInt(cursor, COLUMN_ID));
        data.setTitle(CursorUtil.getString(cursor, COLUMN_TITLE));
        data.setImage(CursorUtil.getString(cursor, COLUMN_IMAGE));
        data.setText(CursorUtil.getString(cursor, COLUMN_TEXT));
        data.setUrl(CursorUtil.getString(cursor, COLUMN_URL));
        data.setTimeCreate(CursorUtil.getString(cursor, COLUMN_TIME));
        return data;
    };

    // 根据对象生成一条数据库记录
    public static ContentValues toContentValues(SysNotice data) {
        ContentValues values = new ContentValues();
        if (data.getId() > 0)
            values.put(COLUMN_ID, data.getId());
        if (!TextUtils.isEmpty(data.getTitle()))
            values.put(COLUMN_TITLE, data.getTitle());
        if (!TextUtils.isEmpty(data.getImage()))
            values.put(COLUMN_IMAGE, data.getImage());
        if (!TextUtils.isEmpty(data.getTitle()))
            values.put(COLUMN_TEXT, data.getText());
        if (!TextUtils.isEmpty(data.getTitle()))
            values.put(COLUMN_URL, data.getUrl());
        if (!TextUtils.isEmpty(data.getTitle()))
            values.put(COLUMN_TIME, data.getTimeCreate());
        return values;
    }
}
